//
//  UFOSXPHeroTimePlayPageViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFOSXPHeroTimePlayPageViewController.h"
#import "UFOHeroTimePlayPageView.h"
#import "UFOSXPHeroTimeVideoModel.h"

#define VIDEOURL @"http://qt.qq.com/php_cgi/cod_video/php/get_url.php?type=1&vid=1026_%@"
@interface UFOSXPHeroTimePlayPageViewController () <UFOHeroTimePlayPageViewDelegate>
@property (nonatomic, retain) NSMutableArray *videoUrlArr;
@property (nonatomic, retain) UFOHeroTimePlayPageView *UFOHeroTimePlayPageV;
@end

@implementation UFOSXPHeroTimePlayPageViewController

- (void)dealloc
{
    [_UFOHeroTimePlayPageV release];
    [_videoUrlArr release];
    [_titleName release];
    [_vid release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    self.navigationItem.title = self.titleName;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)] autorelease];
    [self getData];
    [self createView];
}

// 隐藏navigationBar
- (void)hiddenNavigationBarWithPara:(BOOL)para
{
    self.navigationController.navigationBar.hidden = para;
}

// 返回
- (void)leftAction
{
    [self.UFOHeroTimePlayPageV removeNotification];
    [self.UFOHeroTimePlayPageV removeObserverFromPlayerItem:self.UFOHeroTimePlayPageV.player.currentItem];
    [self.UFOHeroTimePlayPageV.player replaceCurrentItemWithPlayerItem:nil];
    
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// 获取视频数据
- (void)getData
{
    // 5, 网址字符串
    NSString *urlStr = [NSString stringWithFormat:VIDEOURL, self.vid];
    
    // 6, IOS9.0后使用的未明字符转换, 未明字符如中文
    NSString *urlEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    // 4, 创建url
    NSURL *url = [NSURL URLWithString:urlEncode];
    
    // 3, 创建请求对象, request可设置信息如:url, 请求方式(GET/POST), 超时时间等
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 1, 创建NSURLSession对象, 选择默认配置
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    // 2, 创建请求任务, 当异步请求完成会调用blockm 在block内完成数据处理
    NSURLSessionDataTask *getTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSString *str1 = [str stringByReplacingOccurrencesOfString:@"var GET_URL_DATA=" withString:@""];
        NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@";" withString:@""];
        //        NSLog(@"%@", str2);
        NSData *data1 = [str2 dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [dic objectForKey:@"data"];
        self.videoUrlArr = [NSMutableArray array];
        
        for (NSInteger i = 0; i < arr.count; i++) {
            UFOSXPHeroTimeVideoModel *model = [arr objectAtIndex:i];
            [self.videoUrlArr addObject:model];
        }
       
        // 在利用JavaScriptCore与H5交互时出现异常提示：This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.
        // 解决办法是：交互的跳转要在主线程进行
        dispatch_async(dispatch_get_main_queue(), ^{
            self.UFOHeroTimePlayPageV.urlArr = self.videoUrlArr;
      });
    
        [str release];
    }];
    
    // 7, 开始任务
    [getTask resume];
}

// 创建视图
- (void)createView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.UFOHeroTimePlayPageV = [[UFOHeroTimePlayPageView alloc] initWithFrame:self.view.frame];
    self.UFOHeroTimePlayPageV.delegate = self;
    [self.view addSubview:self.UFOHeroTimePlayPageV];
    [_UFOHeroTimePlayPageV release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
